<template>
  <!-- 商品列表 -->
  <div class="app-container">
    <el-form :model="queryParams" :inline="true" label-width="80px">
      <el-form-item label="商品名称" style="margin-left: -10px">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          size="small"
          style="width: 160px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品类型">
        <el-select
          v-model="queryParams.classId"
          placeholder="请选择商品类型"
          size="small"
          style="width: 160px"
        >
        <el-option label="请选择" value="-1" />
          <el-option
            :label="item2.className"
            :value="item2.classId"
            v-for="(item2, index2) in classList"
            :key="index2"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商品供应商" label-width="95px">
        <el-select
          v-model="queryParams.supplierId"
          placeholder="请选择商品供应商"
          size="small"
          style="width: 160px"
        >
         <el-option label="请选择" value="-1" />
          <el-option
            :label="item1.supplierName"
            :value="item1.supplierId"
            v-for="(item1, index1) in supplierList"
            :key="index1"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-button
        type="primary"
        plain
        icon="el-icon-plus"
        size="mini"
        @click="handleAddOrder"
        >新增</el-button
      >
      <el-button
        type="danger"
        plain
        icon="el-icon-refresh"
        size="mini"
        @click="handleClearCache"
        >刷新</el-button
      >
    </el-row>
    <el-table v-loading="loading" :data="typeList">
      <el-table-column label="序号" type="index" align="center" prop="" />
      <el-table-column
        label="商品名称"
        align="center"
        :show-overflow-tooltip="true"
        prop="goodsName"
      >
      </el-table-column>
      <el-table-column
        label="供应商名称"
        align="center"
        :show-overflow-tooltip="true"
        prop="supplierName"
      >
      </el-table-column>
      <el-table-column
        label="商品分类名称"
        align="center"
        :show-overflow-tooltip="true"
        prop="className"
      >
      </el-table-column>

      <el-table-column
        label="销售单价"
        align="center"
        :show-overflow-tooltip="true"
        prop="goodsPricing"
      >
      </el-table-column>
      <el-table-column
        label="采购单价"
        align="center"
        :show-overflow-tooltip="true"
        prop="procurePricing"
      >
      </el-table-column>
      <el-table-column
        label="商品单位"
        align="center"
        :show-overflow-tooltip="true"
        prop="unit"
      >
      </el-table-column>
      <el-table-column
        label="备注"
        align="center"
        :show-overflow-tooltip="true"
        prop="remarks"
      >
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        :show-overflow-tooltip="true"
        prop="createTime"
        width="160"
      >
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        :show-overflow-tooltip="true"
        prop=""
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleGoEdit(scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 新建商品 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="600px"
      append-to-body
      @closed="cancel"
      class="dialog"
    >
      <div class="xian_"></div>
      <el-form
        ref="form"
        :model="ruleForm"
        :rules="rulesForm"
        label-width="80px"
         :hide-required-asterisk="true"
      >
        <el-row>
          <el-col :span="11">
            <el-form-item label="供应商" prop="supplierId" class="item_label">
              <el-select
                v-model="ruleForm.supplierId"
                placeholder="请选择供应商"
                size="small"
                style="width: 100%"
              >
                <el-option
                  :label="item2.supplierName"
                  :value="item2.supplierId"
                  v-for="(item2, index2) in supplierList"
                  :key="index2"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="1" style="opacity: 0"> 测试 </el-col>
          <el-col :span="11">
            <el-form-item label="商品名称" prop="goodsName">
              <el-input
                v-model="ruleForm.goodsName"
                placeholder="请输入商品名称"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="销售单价" prop="goodsPricing">
              <el-input
                v-model="ruleForm.goodsPricing"
                placeholder="请输入销售单价"
                type="number"
              />
            </el-form-item>
          </el-col>
          <el-col :span="1" style="opacity: 0"> 测试 </el-col>
          <el-col :span="11">
            <el-form-item label="采购单价" prop="procurePricing">
              <el-input
                v-model="ruleForm.procurePricing"
                placeholder="请输入采购单价"
                type="number"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="商品分类" prop="classId">
              <el-select
                v-model="ruleForm.classId"
                placeholder="请选择商品类型"
                size="small"
                style="width: 100%"
              >
                <el-option
                  :label="item2.className"
                  :value="item2.classId"
                  v-for="(item2, index2) in classList"
                  :key="index2"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="1" style="opacity: 0"> 测试 </el-col>
          <el-col :span="11">
            <el-form-item label="商品单位" prop="unit">
              <el-input v-model="ruleForm.unit" placeholder="请输入商品单位" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="软件产品">
              <el-select
                v-model="ruleForm.isSoftware"
                placeholder="请选择是否软件产品"
                size="small"
                style="width: 100%"
              >
                <el-option label="是" value="1" key="1" />
                <el-option label="否" value="0" key="0" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="1" style="opacity: 0"> 测试 </el-col>
          <el-col :span="11">
            <el-form-item label="备注" class="item_label">
              <el-input v-model="ruleForm.remarks" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button
          type="primary"
          @click="submitForm('form')"
          v-if="title == '新增商品'"
          >确 定</el-button
        >
        <el-button
          type="primary"
          @click="submitFormEdit('form')"
          v-if="title == '编辑商品'"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  goodsList,
  goodsSupplierList,
  goodsClassList,
  goodsAdd,
  goodsEdit,
} from "@/api/goods/index";
export default {
  data() {
    return {
      goodsId: "", //商品id
      title: "",
      classList: [],
      supplierList: [],
      ruleForm: {
        goodsName: null,
        classId: null,
        supplierId: null,
        goodsPricing: null,
        procurePricing: null,
        unit: null,
        remarks: null,
        isSoftware: "0",
      },
      // 遮罩层
      loading: true,
      // 选中数组
      total: 0,
      // 订单列表
      typeList: [],
      // 是否显示弹出层
      open: false,
      // 搜索状态字典
      statusOptions: [], //支付类型
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsId: null,
        goodsName: null,
        remarks: null,
        supplierId: "-1",
        classId: "-1",
        remarks: null,
      },
      rulesForm: {
        goodsName: [
          { message: "请输入商品名称", trigger: "blur", required: true },
        ],
        unit: [{ message: "请输入商品单位", trigger: "blur", required: true }],
        classId: [
          { message: "请选择商品分类", trigger: "change", required: true },
        ],
        supplierId: [
          { message: "请选择供应商", trigger: "change", required: true },
        ],
        goodsPricing: [
          { message: "请输入销售单价", trigger: "blur", required: true },
        ],
        procurePricing: [
          { message: "请输入采购单价", trigger: "blur", required: true },
        ],
      },
    };
  },
  created() {
    this.getList();
    goodsSupplierList({ pageNum: 1, pageSize: 20 }).then((res) => {
      if (res.code == 200) {
        this.supplierList = res.rows;
      }
    });
    goodsClassList({ pageNum: 1, pageSize: 20 }).then((res) => {
      this.classList = res.rows;
    });
  },
  methods: {
    handleGoEdit(row) { //编辑商品
      this.open = true;
      this.title = "编辑商品";
      this.ruleForm = {
        goodsName: row.goodsName,
        classId: row.classId,
        supplierId: row.supplierId,
        goodsPricing: row.goodsPricing,
        procurePricing: row.procurePricing,
        unit: row.unit,
        remarks: row.remarks,
        isSoftware: String(row.isSoftware),
      };
      this.goodsId = row.goodsId;
    },
    submitFormEdit(formName) {
      //编辑商品
      // this.rulesForm.isSoftware = Number(this.rulesForm.isSoftware);
      this.$refs[formName].validate((valid) => {
        if (valid) {
          goodsEdit({ ...this.ruleForm, goodsId: this.goodsId }).then((res) => {
            if (res.code == 200) {
              this.$message({
                message: "编辑商品成功",
                type: "success",
              });
              this.open = false;
              setTimeout(() => {
                this.getList();
                this.ruleForm = {
                  goodsName: null,
                  classId: null,
                  supplierId: null,
                  goodsPricing: null,
                  procurePricing: null,
                  unit: null,
                  isSoftware: "0",
                };
              }, 100);
            } else {
              this.$message({
                message: res.msg,
                type: "warning",
              });
            }
          });
        } else {
          return false;
        }
      });
    },
    submitForm(formName) {
      // this.rulesForm.isSoftware = Number(this.rulesForm.isSoftware);
      this.$refs[formName].validate((valid) => {
        if (valid) {
          goodsAdd(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                message: "新增商品成功",
                type: "success",
              });
              this.open = false;
              setTimeout(() => {
                this.getList();
                this.ruleForm = {
                  goodsName: null,
                  classId: null,
                  supplierId: null,
                  goodsPricing: null,
                  procurePricing: null,
                  unit: null,
                  isSoftware: "0",
                };
              }, 100);
            } else {
              this.$message({
                message: res.msg,
                type: "warning",
              });
            }
          });
        } else {
          return false;
        }
      });
    },
    cancel() {
      //创建取消
      this.open = false;
      this.ruleForm = {
        goodsName: null,
        classId: null,
        supplierId: null,
        goodsPricing: null,
        procurePricing: null,
        unit: null,
        isSoftware: "0",
      };
    },
    handleAddOrder() {
      this.title = "新增商品";
      this.open = true;
      // this.$router.push({ path: "/commodity/goodsAdd" });
    },
    handleClearCache() {
      window.location.reload();
    },
    /** 查询字典类型列表 */
    getList(index) {
      this.loading = true;
      goodsList(this.queryParams).then((response) => {
        this.typeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      //重置事件
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        goodsId: null,
        goodsName: null,
        remarks: null,
        supplierId: "-1",
        classId: "-1",
      };
      this.getList();
    },
  },
};
</script>
<style lang="scss" scoped>
.xian_ {
  height: 1px;
  width: 600px;
  background-color: #eee;
  margin-left: -20px;
  margin-top: -25px;
  margin-bottom: 30px;
}
 .dialog  .item_label ::v-deep .el-form-item__label {
  width: 60px !important;
  Text-align:justify !important;
  text-align-last:justify !important;
  margin-left: 14px ;
}
.el-input--medium {
  margin-bottom: 10px !important;
}
.el-range-editor {
  width: 100% !important;
}
  .dialog-footer {
    display: flex;
    justify-content: center;
  }
.app-container {
  .el-col-11 {
    width: 100%;
  }
  .el-form-item__content {
    position: relative !important;
    span {
      font-size: 16px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: #333333;
      position: absolute;
      top: 1px;
      right: 12px;
    }
  }
  .mb8 {
    .title {
      font-size: 20px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: #333333;
    }
    .create {
      margin-right: 20 px;
      float: right;
    }
  }
}
</style>